package com.woniu113.junk.site.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu113.junk.commons.model.Result;
import com.woniu113.junk.commons.util.AliyunOSS;
import com.woniu113.junk.site.client.UserClient;
import com.woniu113.junk.site.client.dto.AdminInfo;
import com.woniu113.junk.site.client.dto.EditStationIdForm;
import com.woniu113.junk.site.dto.AllFindAllWastePriceDTO;
import com.woniu113.junk.site.dto.AllStationInfoDTO;
import com.woniu113.junk.site.dto.FindAllWastePriceDTO;
import com.woniu113.junk.site.exception.StationInfoException;
import com.woniu113.junk.site.model.GoodsInfo;
import com.woniu113.junk.site.model.StationInfo;
import com.woniu113.junk.site.mapper.StationInfoMapper;
import com.woniu113.junk.site.model.WastePrice;
import com.woniu113.junk.site.param.stationinfo.AddStationInfoParam;
import com.woniu113.junk.site.param.stationinfo.AllStationInfoParam;
import com.woniu113.junk.site.param.stationinfo.UpdateStationInfoParam;
import com.woniu113.junk.site.service.StationInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu113.junk.site.util.Data;
import org.springframework.stereotype.Service;
import org.springframework.ui.context.Theme;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 蜡笔没了小新
 * @since 2024年10月16日
 */
@Service
public class StationInfoServiceImpl extends ServiceImpl<StationInfoMapper, StationInfo> implements StationInfoService {

    @Resource
    private StationInfoMapper stationInfoMapper;
    @Resource
    private StationInfoService stationInfoService;
    @Resource
    private UserClient userClient;
    @Resource
    private AliyunOSS aliyunOSS;

    @Override
    public StationInfo addStationInfo(AddStationInfoParam param) throws Exception{
        //1.判断数据完整性
        if (param.getStationName()==null||param.getStationPhone()==null||param.getCity()==null||param.getAddressName()==null||param.getLongitude()==null||param.getLatitude()==null
                ||param.getPrincipalId()==null||param.getDescription()==null||param.getArea()==null||param.getServiceScope()==null||param.getLicense()==null){
            throw new StationInfoException("请将数据填写完整",5001);
        }
        //2.判断经度\纬度是否合理(大于0)
        if (param.getLongitude().compareTo(new BigDecimal("-180"))<0 || param.getLongitude().compareTo(new BigDecimal("180"))>0){
            throw new StationInfoException("经度不合理",5002);
        }
        if (param.getLatitude().compareTo(new BigDecimal("-90"))<0 || param.getLatitude().compareTo(new BigDecimal("90"))>0){
            throw new StationInfoException("纬度不合理",5003);
        }
        //3.判断站点名是否重复
        QueryWrapper<StationInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("station_name",param.getStationName()).ne("station_state", Data.STATION_STATE_CLOSE);
        StationInfo stationInfo = stationInfoMapper.selectOne(queryWrapper);
        if (stationInfo!=null){//不为空,表示站点名重复
            throw new StationInfoException("该站点名已存在",5004);
        }
        //4.判断站点联系方式是否重复
        QueryWrapper<StationInfo> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("station_phone",param.getStationPhone()).ne("station_state", Data.STATION_STATE_CLOSE);
        StationInfo stationInfo1 = stationInfoMapper.selectOne(queryWrapper1);
        if (stationInfo1!=null){//不为空,表示联系方式重复
            throw new StationInfoException("该联系方式已存在",5005);
        }
        //5.判断负责人是否存在(存在且其中站点id为空)
        Result<AdminInfo> adminInfo = userClient.selectAdminById2(param.getPrincipalId());
        if (adminInfo==null){
            throw new StationInfoException("不存在该负责人",5006);
        }
        //6.处理图片
        String filePath = aliyunOSS.ProcessPicture(param.getLicense());
        //8.新增站点信息
        StationInfo stationInfo2 = new StationInfo();
        BeanUtil.copyProperties(param,stationInfo2);//同属性赋值
        stationInfo2.setStationState(Data.STATION_STATE_OPEN);
        stationInfo2.setPrincipalName(adminInfo.getData().getRealName());
        stationInfo2.setLicense(filePath);
        stationInfoService.save(stationInfo2);
        //7.修改管理员信息表中的站点id
        EditStationIdForm editStationIdForm = new EditStationIdForm();
        editStationIdForm.setAdminId(param.getPrincipalId());
        editStationIdForm.setStationId(stationInfo2.getStationId());
        userClient.editStationId(editStationIdForm);
        return stationInfo2;
    }

    @Override
    public void updateStationInfo(UpdateStationInfoParam param) {
        //1.判断数据完整性(要修改的字段不能为空,所以前端传入的值就不能为空)
        if (param.getStationId()!=null||param.getStationName()!=null||param.getStationPhone()!=null||param.getPrincipalId()!=null||param.getDescription()!=null||param.getServiceScope()!=null){
            throw new StationInfoException("请将数据填写完整",5001);
        }
        //2.判断站点是否存在
        QueryWrapper<StationInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("station_id",param.getStationId()).ne("station_state",Data.STATION_STATE_CLOSE);
        StationInfo stationInfo = stationInfoMapper.selectOne(queryWrapper);
        if (stationInfo==null){
            throw new StationInfoException("该站点不存在",5007);
        }
        //3.判断站点联系方式是否已存在
        QueryWrapper<StationInfo> queryWrapper1 = new QueryWrapper<>();
        queryWrapper.eq("station_phone",param.getStationPhone()).ne("station_state", Data.STATION_STATE_CLOSE);
        StationInfo stationInfo1 = stationInfoMapper.selectOne(queryWrapper1);
        if (stationInfo1!=null){//不为空,表示联系方式重复
            throw new StationInfoException("该联系方式已存在",5005);
        }
        //4.判断负责人id是否已存在负责站点
        Result<AdminInfo> adminInfo = userClient.selectAdminById(param.getPrincipalId());
        if (adminInfo!=null){
            throw new StationInfoException("该负责人已经存在负责站点",5008);
        }
        //5.执行修改操作
        stationInfo.setStationName(param.getStationName());
        stationInfo.setStationPhone(param.getStationPhone());
        stationInfo.setPrincipalId(param.getPrincipalId());
        stationInfo.setDescription(param.getDescription());
        stationInfo.setServiceScope(param.getServiceScope());
        stationInfoService.updateById(stationInfo);
    }

    @Override
    public void temporarilyCloseStationInfo(Integer stationId) {
        //1.判断数据完整性
        if (stationId==null){
            throw new StationInfoException("请将数据填写完整",5001);
        }
        //2.判断该stationId是否存在对应的站点数据,是否符合暂停营业的状态条件
        QueryWrapper<StationInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("station_id",stationId).eq("station_state",Data.STATION_STATE_OPEN);
        StationInfo stationInfo = stationInfoMapper.selectById(queryWrapper);
        if (stationInfo==null){
            throw new StationInfoException("该站点不符合暂停营业条件",5009);
        }
        //3.修改状态为暂停营业
        stationInfo.setStationState(Data.STATION_STATE_TEMPORARILY_CLOSE);
        stationInfoService.updateById(stationInfo);
    }

    @Override
    public void closeStationInfo(Integer stationId) {
        //1.判断数据完整性
        if (stationId==null){
            throw new StationInfoException("请将数据填写完整",5001);
        }
        //2.判断该stationId是否存在对应的站点数据,是否符合关闭站点的状态条件
        QueryWrapper<StationInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("station_id",stationId).ne("station_state",Data.STATION_STATE_CLOSE);
        StationInfo stationInfo = stationInfoMapper.selectById(queryWrapper);
        if (stationInfo==null){
            throw new StationInfoException("该站点不符合关闭站点条件",5010);
        }
        //3.修改状态为已关闭
        stationInfo.setStationState(Data.STATION_STATE_CLOSE);
        stationInfoService.updateById(stationInfo);
    }

    @Override
    public StationInfo findFatherStationById(Integer stationId) {
        //1.验证数据完整性
        if (stationId==null){
            throw new StationInfoException("请将数据填写完整",5001);
        }
        //2.验证子站点是否存在
        QueryWrapper<StationInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("station_id",stationId).eq("station_state",Data.STATION_STATE_OPEN);
        StationInfo stationInfo = stationInfoMapper.selectOne(queryWrapper);
        if (stationInfo==null){
            throw new StationInfoException("该站点不存在",5007);
        }
        //3.获取子站点的城市和区县
        String city = stationInfo.getCity();
        String county = stationInfo.getCounty();
        QueryWrapper<StationInfo> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("city",city).eq("county",county).eq("station_state",Data.STATION_STATE_OPEN).eq("station_type",Data.STATION_TYPE_FATHER);
        StationInfo stationInfo1 = stationInfoMapper.selectOne(queryWrapper1);
        if (stationInfo1==null){
            throw new StationInfoException("该站点不存在区站点",5011);
        }
        return stationInfo1;
    }

    @Override
    public List<StationInfo> findChildStationById(Integer stationId) {
        //1.判断数据完整性
        if (stationId==null){
            throw new StationInfoException("请将数据填写完整",5001);
        }
        //2.判断区站点是否存在
        StationInfo stationInfo = stationInfoService.getById(stationId);
        if (stationInfo.getStationState()!=Data.STATION_STATE_OPEN){
            throw new StationInfoException("该站点不存在",5007);
        }
        //3.查询出该区站点的所有子站点
        List<StationInfo> childStations = stationInfoService.findChildStationByCityAndCounty(stationInfo.getCity(), stationInfo.getCounty());
        return childStations;
    }

    @Override
    public List<StationInfo> findChildStationByCityAndCounty(String city, String county) {
        //1.判断数据完整性
        if (city==null||county==null){
            throw new StationInfoException("请将数据填写完整",5001);
        }
        //2.根据(城市/区县)查询站点类型为子站点的站点信息
        QueryWrapper<StationInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("city",city).eq("county",county).eq("station_state",Data.STATION_STATE_OPEN).eq("station_type",Data.STATION_TYPE_CHILD);
        List<StationInfo> stationInfos = stationInfoMapper.selectList(queryWrapper);
        return stationInfos;
    }

    @Override
    public StationInfo findFatherStationByCityAndCounty(String city, String county) {
        //1.判断数据完整性
        if (city==null||county==null){
            throw new StationInfoException("请将数据填写完整",5001);
        }
        //2.根据(城市/区县)查询站点类型为区站点的站点信息
        QueryWrapper<StationInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("city",city).eq("county",county).eq("station_state",Data.STATION_STATE_OPEN).eq("station_type",Data.STATION_TYPE_FATHER);
        StationInfo stationInfo = stationInfoMapper.selectOne(queryWrapper);
        return stationInfo;
    }

    @Override
    public List<StationInfo> findFatherStation() {
        QueryWrapper<StationInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("station_state",Data.STATION_STATE_OPEN).eq("station_type",Data.STATION_TYPE_FATHER);
        List<StationInfo> stationInfos = stationInfoMapper.selectList(queryWrapper);
        return stationInfos;
    }

    @Override
    public AllStationInfoDTO findAll(AllStationInfoParam param, Integer offset) {
        QueryWrapper<StationInfo> queryWrapper = new QueryWrapper<>();
        if (param.getStationName()!=null && !param.getStationName().equals("")){
            queryWrapper.eq("station_name",param.getStationName());
        }
        if (param.getStationPhone()!=null && !param.getStationPhone().equals("")){
            queryWrapper.eq("station_phone",param.getStationPhone());
        }
        int size =Math.toIntExact(stationInfoMapper.selectCount(queryWrapper));
        queryWrapper.last("LIMIT " + offset + ", " + param.getPageSize());
        List<StationInfo> infos = stationInfoMapper.selectList(queryWrapper);
        AllStationInfoDTO dto = new AllStationInfoDTO();
        dto.setList(infos);
        dto.setTotal(size);
        return dto;
    }
}
